/* reads gpio line state, similar to 'echo /proc/gpio/GPIOXX' on gumstix
 * display results in human-readable form on stdout
 * args: line number
 *
 * author: maciek gajewski
 *  date: 15.02.2005
 * license: GPL
 */
 
#include <stdio.h>
#include "libgpio.h"

/* ------------------------- main ------------------------ */
int main(int argc, char** argv)
{
	int line;
	gpio_state state;
	
		/* arg test� */
	if(argc<2){
		fprintf(stderr, "Usage: gpioread <line>\n");
		return 1;
	}
		/* parsing line number */
	if(sscanf(argv[1], "%d", &line) != 1){
		fprintf(stderr, "gpioread: '%s' is not valid GPIO line number\n", argv[1]);
		return 2;
	}

		/* read state */
	if( gpio_init() ){
		fprintf(stderr, "gpioread: couldn't initialize libgpio\n");
		return 3;
	}
	if( gpio_get(line, &state) ){
		fprintf(stderr, "gpioread: couldn;t read line state\n");
		return 3;
	}
	
		/* display */
	printf("line: %d\nfunction: %d\ndirection: %s\nstate: %d\n", 
		line, state.function, (state.direction==LG_GPIO_IN)?"in":"out", state.state);
		
	return 0;
}
